#include "StdAfx.h"
#include "SkinManager.h"
#include "xmlparser.h"
#include "XmlHelper.h"

#include "PathUtils.h"

#include "SkinableGUIWnd.h"
#include "SkinableGUIBitmapButton.h"
#include "SkinableGUIButton.h"
#include "SkinableGUIListBox.h"
#include "SkinableGUIComboBox.h"

using namespace SkinableGUI;

static CSkinManager g_SkinManager;

CSkinManager::CSkinManager(void) :
m_bInitialized(FALSE)
{
}

CSkinManager::~CSkinManager(void)
{
}

BOOL CSkinManager::LoadXmlSkinFile(const CString & xmlPath,
								   SkinWindow & skinWindow)
{
	if (!m_bInitialized)
		return FALSE;

	CString file_path;

	if (PathUtils::IsPathRooted(xmlPath))
	{
		file_path = xmlPath;
	}
	else
	{
		file_path.AppendFormat(_T("%s\\%s"), 
			GetWindowPath(), xmlPath);
	}

	CString xmlContent;
	
	if (!PathUtils::GetFileContent(file_path, xmlContent))
		return FALSE;

	XMLNode node = XMLNode::parseString(RemoveXmlHeader(xmlContent));

	SetDefaultValue(skinWindow);

	return HandleXmlNode(&node, skinWindow);
}

BOOL CSkinManager::HandleXmlNode(void * pNode,
								 SkinWindow & skinWindow)
{
	XMLNode * pXmlNode = static_cast<XMLNode *>(pNode);

	CString name = pXmlNode->getName();

	if (name.CompareNoCase(_T("SkinWindow")) != 0)
	{
		return FALSE;
	}

	if (!LoadSkinControl(pXmlNode, skinWindow, NULL))
	{
		return FALSE;
	}

	skinWindow.Title = GetNodeValue(pXmlNode, _T("Title"));

	XMLNode styleNode = pXmlNode->getChildNode(_T("WindowStyle"));

	if (!LoadSkinWindowStyle(&styleNode,
		skinWindow.WindowStyle))
	{
		return FALSE;
	}

	skinWindow.WindowStyleSet = true;

	return TRUE;
}

CString CSkinManager::GetNodeValue(void * pNode, LPCTSTR nodeName)
{
	XMLNode * pXmlNode = (XMLNode *)pNode;

	XMLNode node = pXmlNode->getChildNode(nodeName);

	const TCHAR * pVal = node.getText();

	if (pVal == NULL)
		return _T("");

	return pVal;
}

BOOL CSkinManager::LoadSkinControl(void * pNode,
								   SkinControl & skinControl,
			SkinControl * parentSkinControl)
{
	XMLNode * pXmlNode = static_cast<XMLNode *>(pNode);

	int nC = pXmlNode->nChildNode();

	CString name = pXmlNode->getName();

	if (name.CompareNoCase(_T("SkinControl")) != 0 &&
		name.CompareNoCase(_T("SkinWindow")) != 0 &&
		name.CompareNoCase(_T("SkinButton")) != 0 &&
		name.CompareNoCase(_T("SkinBitmapButton")) != 0 &&
		name.CompareNoCase(_T("SkinPictureControl")) != 0 &&
		name.CompareNoCase(_T("SkinListBox")) != 0)
	{
		return FALSE;
	}

	for(int i = 0 ; i < nC ; i++)
	{
		XMLNode node = pXmlNode->getChildNode(i);

		name = node.getName();

		if (name.CompareNoCase(_T("Controls")) == 0)
		{
			int control_count = node.nChildNode();

			for(int j = 0; j < control_count; j++)
			{
				XMLNode control_node = node.getChildNode(j);
				CString control_name = control_node.getName();

				if (control_name.CompareNoCase(_T("SkinButton")) == 0)
				{
					SkinButton * button = new SkinButton();

					if (LoadSkinButton(&control_node, *button, &skinControl))
					{
						skinControl.Controls.push_back(button);
					}
					else
					{
						delete button;
						return FALSE;
					}
				}
				else if (control_name.CompareNoCase(_T("SkinBitmapButton")) == 0)
				{
					SkinBitmapButton * button = new SkinBitmapButton();

					if (LoadSkinBitmapButton(&control_node, *button, &skinControl))
					{
						skinControl.Controls.push_back(button);
					}
					else
					{
						delete button;
						return FALSE;
					}
				}
				else if (control_name.CompareNoCase(_T("SkinPictureControl")) == 0)
				{
					SkinPictureControl * picture = new SkinPictureControl();

					if (LoadSkinPictureControl(&control_node, *picture, &skinControl))
					{
						skinControl.Controls.push_back(picture);
					}
					else
					{
						delete picture;
						return FALSE;
					}
				}
				else if (control_name.CompareNoCase(_T("SkinListBox")) == 0)
				{
					SkinListBox * listBox = new SkinListBox();

					if (LoadSkinListBox(&control_node, *listBox, &skinControl))
					{
						skinControl.Controls.push_back(listBox);
					}
					else
					{
						delete listBox;
						return FALSE;
					}
				}
				else
				{
					SkinControl * childControl = new SkinControl();

					if (LoadSkinControl(&control_node, *childControl, &skinControl))
					{
						skinControl.Controls.push_back(childControl);
					}
					else
					{
						delete childControl;
						return FALSE;
					}
				}
			}
		}
		else if (name.CompareNoCase(_T("BackgroundImage")) == 0)
		{
			if (!LoadSkinImage(&node, skinControl.BackgroundImage))
				return FALSE;

			skinControl.BackgroundImageSet = true;
		}
		else if (name.CompareNoCase(_T("BackgroundColor")) == 0)
		{
			if (!LoadSkinColor(&node, skinControl.BackgroundColor))
				return FALSE;
			skinControl.BackgroundColorSet = true;
		}
		else if (name.CompareNoCase(_T("Font")) == 0)
		{
			if (!LoadSkinFont(&node, skinControl.Font))
				return FALSE;
			skinControl.FontSet = true;
		}
		else if (name.CompareNoCase(_T("Position")) == 0)
		{
			if (!LoadSkinRectangle(&node, skinControl.Position))
				return FALSE;
			skinControl.PositionSet = true;
		}
		else if (name.CompareNoCase(_T("Style")) == 0)
		{
			if (!LoadSkinControlStyle(&node, skinControl.Style))
				return FALSE;

			skinControl.StyleSet = true;
		}
		else if (name.CompareNoCase(_T("Id")) == 0)
		{
			CString nodeValue = node.getText();

			int v = _ttoi(nodeValue);

#ifndef _WIN32_WCE
			if (errno != ERANGE && errno != EINVAL &&
				v != LONG_MIN  && v != INT_MAX && v != INT_MIN)
			{
#endif
				skinControl.Id = v;

#ifndef _WIN32_WCE
			}
			else
			{
				return FALSE;
			}
#endif
		}
		else if (name.CompareNoCase(_T("TransparentBackground")) == 0)
		{
			CString nodeValue = node.getText();

			if (nodeValue.CompareNoCase(_T("true")) == 0)
			{
				skinControl.TransparentBackground = true;
			}
			else
			{
				skinControl.TransparentBackground = false;
			}

			skinControl.TransparentBackgroundSet  = true;
		}
	}

	skinControl.ParentControl = parentSkinControl;

	return TRUE;
}

BOOL CSkinManager::LoadSkinWindowStyle(void * pNode,
									   SkinWindowStyle & skinWindowStyle)
{
	XMLNode * pXmlNode = static_cast<XMLNode *>(pNode);

	int nC = pXmlNode->nChildNode();

	for(int i = 0 ; i < nC ; i++)
	{
		XMLNode node = pXmlNode->getChildNode(i);

		CString nodeValue = node.getText();

		nodeValue.Trim();

		CString name = node.getName();

		bool val = false;

		if (nodeValue.CompareNoCase(_T("true")) == 0)
		{
			val = true;
		}

		if (name.CompareNoCase(_T("CaptionBar")) == 0)
		{
			skinWindowStyle.CaptionBar = val;
		}
		else if (name.CompareNoCase(_T("MaximizeBox")) == 0)
		{
			skinWindowStyle.MaximizeBox = val;
		}
		else if (name.CompareNoCase(_T("MinimizeBox")) == 0)
		{
			skinWindowStyle.MinimizeBox = val;
		}
	}

	return TRUE;
}

BOOL CSkinManager::LoadSkinFont(void * pNode,
								SkinFont & skinFont)
{
	XMLNode * pXmlNode = static_cast<XMLNode *>(pNode);

	int nC = pXmlNode->nChildNode();

	for(int i = 0 ; i < nC ; i++)
	{
		XMLNode node = pXmlNode->getChildNode(i);

		CString nodeValue = node.getText();

		nodeValue.Trim();

		CString name = node.getName();

		bool val = false;

		if (nodeValue.CompareNoCase(_T("true")) == 0)
		{
			val = true;
		}

		if (name.CompareNoCase(_T("Name")) == 0)
		{
			skinFont.Name = nodeValue;
		}
		else if (name.CompareNoCase(_T("Size")) == 0)
		{
			int v = _ttoi(nodeValue);

#ifndef _WIN32_WCE
			if (errno != ERANGE && errno != EINVAL &&
				v != LONG_MIN  && v != INT_MAX && v != INT_MIN)
			{
#endif
				skinFont.Size = v;
#ifndef _WIN32_WCE
			}
#endif
		}
		else if (name.CompareNoCase(_T("AntiAlias")) == 0)
		{
			skinFont.AntiAlias = val;
		}
		else if (name.CompareNoCase(_T("UnderLine")) == 0)
		{
			skinFont.UnderLine = val;
		}
		else if (name.CompareNoCase(_T("TextColor")) == 0)
		{
			if (!LoadSkinColor(&node, skinFont.TextColor))
				return FALSE;
			skinFont.TextColorSet = true;
		}
		else if (name.CompareNoCase(_T("BackgroundColor")) == 0)
		{
			if (!LoadSkinColor(&node, skinFont.BackgroundColor))
				return FALSE;
			skinFont.BackgroundColorSet = true;
		}
	}

	return TRUE;
}

BOOL CSkinManager::LoadSkinRectangle(void * pNode,
									 SkinRectangle & skinRectangle)
{
	XMLNode * pXmlNode = static_cast<XMLNode *>(pNode);

	int nC = pXmlNode->nChildNode();

	for(int i = 0 ; i < nC ; i++)
	{
		XMLNode node = pXmlNode->getChildNode(i);

		CString nodeValue = node.getText();

		nodeValue.Trim();

		CString name = node.getName();

		int v = _ttoi(nodeValue);

#ifndef _WIN32_WCE
		if (errno != ERANGE && errno != EINVAL &&
			v != LONG_MIN  && v != INT_MAX && v != INT_MIN)
		{
		}
		else
		{
			continue;
		}
#endif

		if (name.CompareNoCase(_T("Top")) == 0)
		{
			skinRectangle.top = v;
		}
		else if (name.CompareNoCase(_T("Left")) == 0)
		{
			skinRectangle.left = v;
		}
		else if (name.CompareNoCase(_T("Right")) == 0)
		{
			skinRectangle.right = v;
		}
		else if (name.CompareNoCase(_T("Bottom")) == 0)
		{
			skinRectangle.bottom = v;
		}
	}

	return TRUE;
}

BOOL CSkinManager::LoadSkinControlStyle(void * pNode,
										SkinControlStyle & skinControlStyle)
{
	XMLNode * pXmlNode = static_cast<XMLNode *>(pNode);

	return TRUE;
}

BOOL CSkinManager::LoadSkinImage(void * pNode,
								 SkinImage & skinImage)
{
	XMLNode * pXmlNode = static_cast<XMLNode *>(pNode);

	int nC = pXmlNode->nChildNode();

	for(int i = 0 ; i < nC ; i++)
	{
		XMLNode node = pXmlNode->getChildNode(i);

		CString nodeValue = node.getText();

		nodeValue.Trim();

		CString name = node.getName();

		bool val = false;

		if (nodeValue.CompareNoCase(_T("true")) == 0)
		{
			val = true;
		}

		if (name.CompareNoCase(_T("Path")) == 0)
		{
			skinImage.Path = nodeValue;
		}
		else if (name.CompareNoCase(_T("TransparentColor")) == 0)
		{
			if (!LoadSkinColor(&node, skinImage.TransparentColor))
				return FALSE;
		}
		else if (name.CompareNoCase(_T("Transparent")) == 0)
		{
			skinImage.Transparent = val;
		}
	}

	return TRUE;
}

BOOL CSkinManager::LoadSkinColor(void * pNode,
								 SkinColor & skinColor)
{
	XMLNode * pXmlNode = static_cast<XMLNode *>(pNode);

	int nC = pXmlNode->nChildNode();

	skinColor.R = -1;
	skinColor.G = -1;
	skinColor.B = -1;

	for(int i = 0 ; i < nC ; i++)
	{
		XMLNode node = pXmlNode->getChildNode(i);

		CString nodeValue = node.getText();

		nodeValue.Trim();

		CString name = node.getName();

		int v = _ttoi(nodeValue);

#ifndef _WIN32_WCE
		if (errno != ERANGE && errno != EINVAL &&
			v != LONG_MIN  && v != INT_MAX && v != INT_MIN)
		{
		}
		else
		{
			continue;
		}
#endif

		if (name.CompareNoCase(_T("R")) == 0)
		{
			skinColor.R = v;
		}
		else if (name.CompareNoCase(_T("G")) == 0)
		{
			skinColor.G = v;
		}
		else if (name.CompareNoCase(_T("B")) == 0)
		{
			skinColor.B = v;
		}
	}

	return skinColor.R >= 0 && 
		skinColor.G >= 0 &&
		skinColor.B >= 0;
}

void CSkinManager::SetDefaultValue(SkinWindow & skinWindow)
{
	COLORREF cr = GetSysColor(COLOR_WINDOW);

	skinWindow.BackgroundColor.R = GetRValue(cr);
	skinWindow.BackgroundColor.G = GetGValue(cr);
	skinWindow.BackgroundColor.B = GetBValue(cr);

	skinWindow.WindowStyle.CaptionBar = FALSE;
	skinWindow.WindowStyle.MaximizeBox = FALSE;
	skinWindow.WindowStyle.MinimizeBox = FALSE;
}

CSkinManager * CSkinManager::GetInstance()
{
	return &g_SkinManager;
}

BOOL CSkinManager::LoadSkinButton(void * pNode,
								  SkinButton & skinButton,
			SkinControl * parentSkinControl)
{
	if (!LoadSkinControl(pNode, skinButton, parentSkinControl))
	{
		return FALSE;
	}

	XMLNode * pXmlNode = static_cast<XMLNode *>(pNode);

	int nC = pXmlNode->nChildNode();

	for(int i = 0 ; i < nC ; i++)
	{
		XMLNode node = pXmlNode->getChildNode(i);

		CString nodeValue = node.getText();

		nodeValue.Trim();

		CString name = node.getName();

		if (name.CompareNoCase(_T("ActionName")) == 0)
		{
			skinButton.ActionName = nodeValue;
			skinButton.ActionNameSet = true;
		}
		else if (name.CompareNoCase(_T("ButtonText")) == 0)
		{
			skinButton.ButtonText = nodeValue;
			skinButton.ButtonTextSet = true;
		}
	}

	return TRUE;
}

BOOL CSkinManager::LoadSkinBitmapButton(void * pNode,
										SkinBitmapButton & skinBitmapButton,
			SkinControl * parentSkinControl)
{
	if (!LoadSkinButton(pNode, skinBitmapButton, parentSkinControl))
		return FALSE;

	XMLNode * pXmlNode = static_cast<XMLNode *>(pNode);

	int nC = pXmlNode->nChildNode();

	for(int i = 0 ; i < nC ; i++)
	{
		XMLNode node = pXmlNode->getChildNode(i);

		CString name = node.getName();

		if (name.CompareNoCase(_T("Normal")) == 0)
		{
			if (!LoadSkinImage(&node, skinBitmapButton.Normal))
				return FALSE;

			skinBitmapButton.NormalSet = true;
		}
		else if (name.CompareNoCase(_T("Selected")) == 0)
		{
			if (!LoadSkinImage(&node, skinBitmapButton.Selected))
				return FALSE;
			skinBitmapButton.SelectedSet = true;
		}
		else if (name.CompareNoCase(_T("Focus")) == 0)
		{
			if (!LoadSkinImage(&node, skinBitmapButton.Focus))
				return FALSE;
			skinBitmapButton.FocusSet = true;
		}
		else if (name.CompareNoCase(_T("Disabled")) == 0)
		{
			if (!LoadSkinImage(&node, skinBitmapButton.Disabled))
				return FALSE;
			skinBitmapButton.DisabledSet = true;
		}
	}

	return TRUE;
}

BOOL CSkinManager::LoadSkinPictureControl(void * pNode,
										  SkinPictureControl & skinPicture,
			SkinControl * parentSkinControl)
{
	if (!LoadSkinControl(pNode, skinPicture, parentSkinControl))
	{
		return FALSE;
	}

	XMLNode * pXmlNode = static_cast<XMLNode *>(pNode);

	int nC = pXmlNode->nChildNode();

	for(int i = 0 ; i < nC ; i++)
	{
		XMLNode node = pXmlNode->getChildNode(i);

		CString name = node.getName();

		if (name.CompareNoCase(_T("Image")) == 0)
		{
			if (!LoadSkinImage(&node, skinPicture.Image))
				return FALSE;

			skinPicture.ImageSet = true;
		}
	}

	return TRUE;
}

BOOL CSkinManager::LoadSkinListBox(void * pNode,
								   SkinListBox & skinListBox,
			SkinControl * parentSkinControl)
{
	if (!LoadSkinControl(pNode, skinListBox, parentSkinControl))
	{
		return FALSE;
	}

	XMLNode * pXmlNode = static_cast<XMLNode *>(pNode);

	int nC = pXmlNode->nChildNode();

	for(int i = 0 ; i < nC ; i++)
	{
		XMLNode node = pXmlNode->getChildNode(i);

		CString nodeValue = node.getText();

		nodeValue.Trim();

		CString name = node.getName();

		bool val = false;

		if (nodeValue.CompareNoCase(_T("true")) == 0)
		{
			val = true;
		}

		if (name.CompareNoCase(_T("HorzScrollBar")) == 0)
		{
			if (!LoadSkinScrollBar(&node, skinListBox.HorzScrollBar))
				return FALSE;

			skinListBox.HorzScrollBarSet = true;
		}
		else if (name.CompareNoCase(_T("VertScrollBar")) == 0)
		{
			if (!LoadSkinScrollBar(&node, skinListBox.VertScrollBar))
				return FALSE;
			skinListBox.VertScrollBarSet = true;
		}
		else if (name.CompareNoCase(_T("VertScroll")) == 0)
		{
			skinListBox.VertScroll = val;
		}
		else if (name.CompareNoCase(_T("HorzScroll")) == 0)
		{
			skinListBox.HorzScroll = val;
		}
	}

	return TRUE;
}

BOOL CSkinManager::LoadSkinScrollBar(void * pNode,
									 SkinScrollBar & skinScrollBar)
{
	XMLNode * pXmlNode = static_cast<XMLNode *>(pNode);

	int nC = pXmlNode->nChildNode();

	for(int i = 0 ; i < nC ; i++)
	{
		XMLNode node = pXmlNode->getChildNode(i);

		CString name = node.getName();

		if (name.CompareNoCase(_T("IncImage")) == 0)
		{
			if (!LoadSkinImage(&node, skinScrollBar.IncImage))
				return FALSE;
			skinScrollBar.IncImageSet = true;
		}
		else if (name.CompareNoCase(_T("DecImage")) == 0)
		{
			if (!LoadSkinImage(&node, skinScrollBar.DecImage))
				return FALSE;
			skinScrollBar.DecImageSet = true;
		}
		else if (name.CompareNoCase(_T("ThumbImage")) == 0)
		{
			if (!LoadSkinImage(&node, skinScrollBar.ThumbImage))
				return FALSE;
			skinScrollBar.ThumbImageSet = true;
		}
		else if (name.CompareNoCase(_T("BackgroundImage")) == 0)
		{
			if (!LoadSkinImage(&node, skinScrollBar.BackgroundImage))
				return FALSE;
			skinScrollBar.BackgroundImageSet = true;
		}
		else if (name.CompareNoCase(_T("Position")) == 0)
		{
			if (!LoadSkinScrollBarPos(&node, skinScrollBar.Position))
				return FALSE;
			skinScrollBar.PositionSet = true;
		}
	}

	return TRUE;
}

BOOL CSkinManager::UpdateWindowStyle(CWnd * pWnd, const SkinWindowStyle & windowStyle)
{
	if (!m_bInitialized)
		return FALSE;
	DWORD addStyle = 0;
	DWORD removeStyle = 0;

	//if (windowStyle.MaximizeBox)
	//	addStyle = WS_MAXIMIZEBOX;
	//else
	//	removeStyle = WS_MAXIMIZEBOX;

	//if (windowStyle.MinimizeBox)
	//	addStyle |= WS_MINIMIZEBOX;
	//else
	//	removeStyle |= WS_MINIMIZEBOX;

	//if (windowStyle.CaptionBar)
	//	addStyle |= WS_CAPTION;
	//else
	//	removeStyle |= WS_CAPTION;

	//return pWnd->ModifyStyle(addStyle, removeStyle);

	return TRUE;
}

BOOL CSkinManager::UpdateControlStyle(CWnd * pWnd, 
									  const SkinControlStyle & controlStyle)
{
	if (!m_bInitialized)
		return FALSE;
	return TRUE;
}

CWnd * CSkinManager::CreateControl(CWnd * pParentWnd, 
								   const SkinControl & control)
{
	if (!m_bInitialized)
		return NULL;

	switch(control.ControlType)
	{
	case SkinableGUI::Window:
	case SkinableGUI::Control:
		{
			CSkinableGUIWnd * pWnd = 
				new CSkinableGUIWnd((SkinControl *)&control);

			pWnd->Create(_T("SkinableGUIWindowClass"),
				NULL,
				WS_CHILD,
				control.Position,
				pParentWnd,
				control.Id);

			return pWnd;
		}
		break;
	case SkinableGUI::BitmapButton:
		{
			CSkinableGUIBitmapButton * pButton =
				new CSkinableGUIBitmapButton((SkinBitmapButton *)&control);

			pButton->Create(NULL,
				WS_CHILD | BS_PUSHBUTTON,
				control.Position,
				pParentWnd,
				control.Id);

			return pButton;
		}
		break;
	case SkinableGUI::Button:
		{
			CSkinableGUIButton * pButton =
				new CSkinableGUIButton((SkinButton *)&control);

			pButton->Create(NULL,
				WS_CHILD | BS_PUSHBUTTON,
				control.Position,
				pParentWnd,
				control.Id);

			return pButton;
		}
		break;
	case SkinableGUI::ListBox:
		{
			CSkinableGUIListBox * pListBox =
				new CSkinableGUIListBox((SkinListBox *)&control);

			pListBox->Create(WS_CHILD,
				control.Position,
				pParentWnd,
				control.Id);

			return pListBox;
		}
		break;
	case SkinableGUI::Picture:
		break;
	default:
		break;
	}
	return NULL;
}

BOOL CSkinManager::LoadFont(const SkinFont & skinFont, CFont & font)
{
	if (!m_bInitialized)
		return FALSE;

	LOGFONT lf;
	lf.lfHeight = skinFont.Size * 10;
	lf.lfWidth = 0;
	lf.lfEscapement = 0;
	lf.lfOrientation = 0;
	lf.lfWeight = FW_NORMAL;
	lf.lfItalic = FALSE;
	lf.lfUnderline = skinFont.UnderLine ? TRUE : FALSE;
	lf.lfStrikeOut = FALSE;
	lf.lfCharSet = ANSI_CHARSET;
	lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
	lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;

	if (skinFont.AntiAlias)
	{
		lf.lfQuality = ANTIALIASED_QUALITY | CLEARTYPE_QUALITY;
	}
	else
	{
		lf.lfQuality = DEFAULT_QUALITY;
	}

	lf.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
#ifdef _WIN32_WCE
	_tcscpy(lf.lfFaceName, skinFont.Name);
#else
	_tcscpy_s(lf.lfFaceName, skinFont.Name);
#endif

	return font.CreatePointFontIndirect(&lf);
}

BOOL CSkinManager::LoadImage(const SkinImage & skinImage, CSkinableGUIImage & image)
{
	if (!m_bInitialized)
		return FALSE;

	return image.Load((SkinImage *)&skinImage);
}

BOOL CSkinManager::InitializeSkin(const CString & skinFolder)
{
	if (!PathUtils::DoesDirectoryExists(skinFolder))
		return FALSE;

	m_strSkinPath = skinFolder;

	m_strWindowPath.Format(_T("%s\\%s"),
		skinFolder, _T("Window"));
	m_strMediaPath.Format(_T("%s\\%s"),
		skinFolder, _T("Media"));
	m_strLanguagePath.Format(_T("%s\\%s"),
		skinFolder, _T("Language"));
	m_strFontPath.Format(_T("%s\\%s"),
		skinFolder, _T("Font"));

	WNDCLASS    wc;

	ZeroMemory(&wc, sizeof wc);
	wc.lpfnWndProc  = DefWindowProc;
	wc.hInstance    = AfxGetInstanceHandle();
	wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH );
	wc.lpszMenuName = NULL;
	wc.lpszClassName = _T("SkinableGUIWindowClass");
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	RegisterClass( &wc );

	return m_bInitialized = PathUtils::DoesDirectoryExists(m_strWindowPath) &&
		PathUtils::DoesDirectoryExists(m_strMediaPath) &&
		PathUtils::DoesDirectoryExists(m_strLanguagePath) &&
		PathUtils::DoesDirectoryExists(m_strFontPath);
}

void CSkinManager::ReleaseSkin()
{
}

BOOL CSkinManager::LoadSkinScrollBarPos(void * pNode,
	SkinScrollBarPos & skinScrollBarPos)
{
	XMLNode * pXmlNode = static_cast<XMLNode *>(pNode);

	int nC = pXmlNode->nChildNode();

	for(int i = 0 ; i < nC ; i++)
	{
		XMLNode node = pXmlNode->getChildNode(i);

		CString nodeValue = node.getText();

		nodeValue.Trim();

		CString name = node.getName();

		if (name.CompareNoCase(_T("Size")) == 0)
		{
			if (!LoadSkinRectangle(&node, skinScrollBarPos.Size))
				return FALSE;

			skinScrollBarPos.SizeSet = true;
		}
		else if (name.CompareNoCase(_T("Dock")) == 0)
		{
			if (nodeValue.CompareNoCase(_T("left")) == 0)
			{
				skinScrollBarPos.Dock = SkinableGUI::Left;
			}
			else if (nodeValue.CompareNoCase(_T("top")) == 0)
			{
				skinScrollBarPos.Dock = SkinableGUI::Top;
			}
			else if (nodeValue.CompareNoCase(_T("right")) == 0)
			{
				skinScrollBarPos.Dock = SkinableGUI::Right;
			}
			else if (nodeValue.CompareNoCase(_T("bottom")) == 0)
			{
				skinScrollBarPos.Dock = SkinableGUI::Bottom;
			}
			else
			{
				return FALSE;
			}

			skinScrollBarPos.DockSet = true;
		}
	}

	return TRUE;
}
